package com.hk.huakangcompany.service.impl;

import com.github.pagehelper.PageHelper;
import com.hk.huakangcompany.mapper.UserMapper;
import com.hk.huakangcompany.service.UserService;
import com.hk.huakangcompany.utils.DateUtils;
import com.hk.huakangcompany.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/8 15:06
 * @Statement: 会员业务层
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll(Integer page, Integer limit, User t) {
        PageHelper.startPage(page,limit);
        if (t.getHobby() != null && t.getHobby() != "") {
            String[] hobbys = t.getHobby().split(",");
            t.setHobbys(hobbys);
        }
        List<User> userList = userMapper.findAll(t);
        return userList;
    }

    @Override
    public int count(Integer page, Integer limit, User t) {
//        PageHelper.startPage(page,limit);
        int count = userMapper.count(t);
        return count;
    }

    @Override
    public User findById(Integer id) {
        User user = userMapper.findById(id);
        return user;
    }

    @Override
    public int save(User user) {
        String dateTimeString = DateUtils.dateTimeString(new Date());
        user.setGmtCreate(dateTimeString);
        user.setGmtModified(dateTimeString);
        int save = userMapper.save(user);
        if (save == 1) {
            System.out.println("添加成功");
        }
        return save;

    }

    @Override
    public int update(User user) {
//        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        user.setGmtModified(dateTimeString);
        int update = userMapper.update(user);
        if (update == 1) {
            System.out.println("会员信息修改成功");
        }
        return update;
    }

    @Override
    public int updatePassWord(User user) {
//        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        user.setGmtModified(dateTimeString);
        int updatePassWord = userMapper.updatePassWord(user);
        if (updatePassWord == 1) {
            System.out.println("会员密码修改成功");
        }
        return updatePassWord;
    }

    @Override
    public int updateState(User user) {
//        修改日期时间
        String dateTimeString = DateUtils.dateTimeString(new Date());
        user.setGmtModified(dateTimeString);
        int updateState = userMapper.updateState(user);
        if (updateState == 1) {
            System.out.println("状态修改成功");
        }
        return updateState;
    }

    @Override
    public int delete(Integer id) {
        int delete = userMapper.delete(id);
        if (delete == 1) {
            System.out.println("会员删除成功");
        }
        return delete;
    }

    @Override
    public int findByEmail(String email) {
        User user = userMapper.findByEmail(email);
        if (user != null && user.getId() != 0) {
            System.out.println("邮箱已存在，请更换邮箱！");
            return 1;
        }
        return 0;
    }
}
